<?php
class indexController extends baseController{

	public function __construct(){
		parent::__construct();
		$this->lockFile = BASE_PATH . 'cache/install.lock';
		if(file_exists($this->lockFile)){
			$this->redirect(__ROOT__.'/');
		}
	}

	public function index(){
		$this->display('index');
	}

	public function test(){

		if(phpversion()<5){
			die('本系统需要PHP5+MYSQL >=4.1环境，当前PHP版本为：'.phpversion());
		}

        $this->phpv = @ phpversion();
        $this->os = @PHP_OS;
		$this->os = @php_uname();
		$tmp = function_exists('gd_info') ? gd_info() : array();
        $this->server = $_SERVER["SERVER_SOFTWARE"];
        $this->host = (empty ($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_HOST"] : $_SERVER["SERVER_ADDR"]);
        $this->name = $_SERVER["SERVER_NAME"];
        $this->max_execution_time = ini_get('max_execution_time');
        $this->allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
        $this->allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
        $this->safe_mode = (ini_get('safe_mode') ? '<font color=red>[×]On</font>' : '<font color=green>[√]Off</font>');

		$err=0;
		if(empty($tmp['GD Version'])){
			$this->gd =  '<font color=red>[×]Off</font>' ;
			$err++;
		}else{
			$this->gd =  '<font color=green>[√]On</font> '.$tmp['GD Version'];
		}
		if(function_exists('mysql_connect')){
			$this->mysql = '<font color=green>[√]On</font>';
		}else{
			$this->mysql = '<font color=red>[×]Off</font>';
			$err++;
		}
		if(ini_get('file_uploads')){
			$this->uploadSize = '<font color=green>[√]On</font> 文件限制:'.ini_get('upload_max_filesize');
		}else{
			$this->uploadSize = '禁止上传';
		}
		if(function_exists('session_start')){
			$this->session = '<font color=green>[√]On</font>' ;
		}else{
			$this->session = '<font color=red>[×]Off</font>';
			$err++;
		}
		$this->err=$err;
		$dir_list=array(
					'/',
					'upload',							
        			'protected/config.php',
					'protected/cache',
					'protected/cache/app_cache',
			);
         $this->dir_list=$dir_list;

		$this->display('test');
	}

	public function data(){
		$this->display('data');
	}

	public function test_data(){
			$link=@mysql_connect($_POST['DB_HOST'].':'.$_POST['DB_PORT'],$_POST['DB_USER'],$_POST['DB_PWD']);
			if(!$link){
				echo '数据库连接失败，请检查连接信息是否正确！';
				return;
			}
			if($_POST['create']==1){
				echo 1;
				return;
			}
			$status=@mysql_select_db($_POST['DB_NAME'],$link);
			if($status){
				echo 1;
			}else{
				echo '数据库连接成功，请先建立数据库！';
			}
			return;
	}

	public function install(){
		$data=in($_POST);
		$config_db=$data['DB'];
		if(!model('install')->create_db($config_db)){
            $this->error('数据库创建失败，请检测本帐号是否有权限！');
            $this->display('error');
            exit;
        }
        
		$DB_PREFIX=$config_db['DB_PREFIX'];
		if(empty($DB_PREFIX)){
			$DB_PREFIX='dux_';
		}
		$db_frame=BASE_PATH.'apps/'.APP_NAME.'/data/frame.sql';
		$db_data=BASE_PATH.'apps/'.APP_NAME.'/data/data.sql';
		$db_testdata=BASE_PATH.'apps/'.APP_NAME.'/data/testdata.sql';

		$sql_frame=Install::mysql($db_frame,'dux_',$DB_PREFIX);
		$sql_data=Install::mysql($db_data,'dux_',$DB_PREFIX);
		$sql_testdata=Install::mysql($db_testdata,'dux_',$DB_PREFIX);

		if(!model('install')->run_sql($config_db,$sql_frame)){
			$this->error('表结构导入失败，请检查后手动删除数据库重新安装！');
            $this->display('error');
		}
		if(!model('install')->run_sql($config_db,$sql_data)){
			$this->error('基础数据导入失败，请检查后手动删除数据库重新安装！');
            $this->display('error');
		}
		if($data['test']){
			model('install')->run_sql($config_db,$sql_testdata);
		}

		$config=model('install')->form_config($data);
        if(api('admin','setting_config_save',array('app'=>BASE_PATH . '/config.php','config'=>$config))){
        	@fopen($this->lockFile, 'w');
        	$this->display('success');
        }else{
        	$this->error('配置文件写入失败，请检测config.php是否有写入权限！');
        	$this->display('error');
        }


	}

}